﻿using Microsoft.AspNetCore.Mvc.Filters;

namespace SmartCity.ProcessEngine.Api.Write.Filters
{
    /// <summary>
    /// 只要实现IExceptionFilter接口,类就变成全局异常过滤器类
    /// </summary>
    public class MyExceptionFilter : IExceptionFilter
    {
        private readonly ILogger<MyExceptionFilter> logger;

        public MyExceptionFilter(ILogger<MyExceptionFilter> logger)
        {
            this.logger = logger;
        }
        /// <summary>
        /// 拦截方法
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnException(ExceptionContext context)
        {
            //1.记录日志
            logger.LogError($"发生全局异常:{context.Exception.Message}");
            //2.标记处理状态
            context.ExceptionHandled = true;
        }
    }
}
